﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;
using System.Data;
using MINERP.DAL.Statistics;


namespace MINERP.BLL.Statistics
{
    public class BoardStatBLL
    {
        public List<Printer_Info> GetPrinterXray()
        {
            List<Printer_Info> outPutList = new List<Printer_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<printer> list = db.printer.Where(p => p.PType == "1").ToList();
                foreach (printer pr in list)
                {
                    Printer_Info printer = new Printer_Info();
                    printer.PrinterID = pr.PrinterID;
                    printer.PName = pr.PName;
                    outPutList.Add(printer);
                }
            }
            return outPutList;

        }

        public List<PaperType_Info> GetAllPaperType()
        {
            List<PaperType_Info> outPutPaperTypeList = new List<PaperType_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var type in db.paperType.Where(t => t.Status == 1))
                {
                    PaperType_Info typeInfo = new PaperType_Info();
                    typeInfo.PaperTypeID = type.PaperTypeID;
                    typeInfo.Name = type.Name;
                    outPutPaperTypeList.Add(typeInfo);
                }

            }
            return outPutPaperTypeList;
        }

        public List<User_Info> GetUserList()
        {
            List<User_Info> outPutUserList = new List<User_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //foreach (var user in db.users.Where(u => u.tb_WorkerGroup.GroupName.Contains("输出") || u.tb_WorkerGroup.GroupName.Contains("拼版")))
                foreach (var user in db.users.Where(u=>u.IsUseful))
                {
                    User_Info u = new User_Info();
                    u.UserID = user.userID;
                    u.UserName = user.UserName;
                    outPutUserList.Add(u);
                }
            }
            return outPutUserList;
        }
        public DataTable GetData(string inPutPrinterid, string inPutPaperTypeid, string inPutUserid, DateTime inPutDateFrom, DateTime inPutDateTo)
        {
            StringBuilder selectSqLStr = new StringBuilder();
            selectSqLStr.Append("select BoardId,board_bk.picPath,board_bk.width/printer.pdpi*2.54 as Width,board_bk.length/printer.pdpi*2.54 as Length,board_bk.length/printer.pdpi*2.54*board_bk.width/printer.pdpi*2.54/10000 as Areas,");
            selectSqLStr.Append(" WasteRate,createTime,printNumber,board_bk.lastPrintTime,printer.Pname as printerName,Papertype.name as papertypeName ,UserName");
            selectSqLStr.Append(" from board_bk,printer,papertype,users");
            //selectSqLStr.Append(" from board_bk,printer,papertype,users,PaperInfo");

            selectSqLStr.Append(" where board_bk.printerId=printer.printerId");
            selectSqLStr.Append(" and board_bk.paperTypeId=Papertype.paperTypeId");
            selectSqLStr.Append(" and board_bk.WorkerID=users.userID");
            //selectSqLStr.Append(" and board_bk.PrintPaperId=PaperInfo.PrintPaperId");
            if (!string.IsNullOrEmpty(inPutPrinterid))
            {
                selectSqLStr.Append(" and printer.printerId in ("+inPutPrinterid+")");
            }
            if (!string.IsNullOrEmpty(inPutPaperTypeid))
            {
                selectSqLStr.Append(" and Papertype.paperTypeId in (" + inPutPaperTypeid + ")");
            }
            if (!string.IsNullOrEmpty(inPutUserid))
            {
                selectSqLStr.Append(" and users.userID in (" + inPutUserid + ")");
            }
            selectSqLStr.Append(" and board_bk.lastPrintTime >'" + inPutDateFrom.ToString("yyyyMMdd HH:mm:ss") + "' and board_bk.lastPrintTime< '" + inPutDateTo.ToString("yyyyMMdd HH:mm:ss") + "'");
            BoardStaDAL dal = new BoardStaDAL();
            return dal.ExceSQL(selectSqLStr.ToString());
            
        }
    }
}
